<?php
namespace app\Index\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Comment extends Controller
{
    /**
     * @return \think\response\View
     * 评价管理
     */
    public function index()
    {
        if(Session::get('login_admin.id')){$user_id=Session::get('login_admin.id');} else{$user_id=Session::get('login_tel.id');}
        $list = Db::name('index_navigation')->select();
        //从数据库里查询友情链接
        $data = Db::table('lt_link')->select();

        $order = Db::table('lt_house_details')
            ->alias('a')
            ->join('lt_order_detials w','a.id = w.house_id')
            ->join('lt_user_reviews t', 'w.id = t.order_id')
            ->where('t.user_id',$user_id)
            ->select();
         if(is_mobile()){
            //手机端
             return view('comment/Mindex', [
                 'data'=>$data,
                 'info'=>$list,
                 'order'=>$order,
             ]);
         }else{
            //电脑端
             return view('comment/index', [
                 'data'=>$data,
                 'info'=>$list,
                 'order'=>$order,
             ]);
         }


    }

    public function commentlist($id)
    {
        $list = Db::name('index_navigation')->select();
        //从数据库里查询友情链接
        $data = Db::table('lt_link')->select();
        $order = Db::table('lt_house_details')
            ->alias('a')
            ->join('lt_order_detials w','a.id = w.house_id')
            ->where('w.id',$id)
            ->find();
        if(is_mobile()){
            //手机端
            return view('comment/Mcommentlist', [
                'data'=>$data,
                'info'=>$list,
                'order'=>$order,
            ]);
        }else{
            //电脑端
            return view('comment/commentlist', [
                'data'=>$data,
                'info'=>$list,
                'order'=>$order,
            ]);
        }

    }

    /**
     * 发布评论
     * @param $id
     */
    public function save(Request $request,$id)
    {
       if(Session::get('login_admin.id')){$user_id=Session::get('login_admin.id');} else{$user_id=Session::get('login_tel.id');}
        $data = $request->post();
        $list = Db::name('order_detials')->where('id',$id)->field('house_id,id')->find();
        $order_id = $list['id'];
        $file = request()->file('upload_path');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/comment');
            if($info){
                // 成功上传后 获取上传信息

                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $path=$info->getSaveName();

            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            };


            //过滤数据f
            $info = [
                "commentinfo" => $data['commentinfo'],
                "order_id" => $order_id,
                "comment_time" =>date('Y-m-d H:i:s', time()),
                "user_id"=>$user_id,
                "image"=>$path,
            ];
        }else{
            //过滤数据f
            $info = [
                "commentinfo" => $data['commentinfo'],
                "order_id" => $order_id,
                "comment_time" =>date('Y-m-d H:i:s', time()),
                "user_id"=>$user_id,
                ];

        }

        $result = Db::name('user_reviews')->insert($info);

        //重定向页面
        if ($result > 0) {
            return $this->success('添加成功', url('index/comment/index'));
        } else {
            return $this->error('添加失败');
        }
    }
}